package cn.ibdsr.human.interceptor;

import cn.ibdsr.human.util.HttpServletUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局拦截器
 * @author chenglx
 * @version V1.0
 * @date 2020/7/7 18:01
 * <p>
 * Date Author Description
 * ------------------------------------------------------
 * 2020/7/7 chenglx init
 */
@Component
public class GlobalInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
        // 将当前处理的HTTP请求、响应对象，添加到线程本地
        HttpServletUtils.setRequest(request);
        HttpServletUtils.setResponse(response);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
                           ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                Object o, Exception e) {
        // 清除当前线程的HTTP请求、响应对象
        HttpServletUtils.removeRequest();
        HttpServletUtils.removeResponse();
    }
}
